/*
 * Copyright © 2020 https://www.lrting.top/ All rights reserved.
 */


package com.zh.ch.bigdata.flink.sqlsubmit;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: bigDataToAi
 * @date: 2022/7/8
 * @description:
 * @modifiedBy:
 * @version: 1.0
 */
public class FileUtils {

    private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class);

    public static List<String> readLine(String filePath) {
        List<String> lines = new ArrayList<>();

        try {
            File file = new File(filePath);
            InputStreamReader input = new InputStreamReader(Files.newInputStream(file.toPath()));
            BufferedReader bf = new BufferedReader(input);
            String str;
            while ((str = bf.readLine()) != null) {
                lines.add(str);
            }
            bf.close();
            input.close();
        } catch (IOException e) {
            LOG.error("文件读取失败，当前文件路径为 {}", filePath);
        }
        return lines;
    }

    public static String read(String filePath) throws IOException {
        try {
            File jsonFile = new File(filePath);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(Files.newInputStream(jsonFile.toPath()), StandardCharsets.UTF_8);
            int ch;
            StringBuilder sb = new StringBuilder();
            while ((ch = fileReader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            return sb.toString();
        }
        catch (IOException ioException) {
            LOG.error("文件读取失败，文件路径为 {}", filePath);
            throw ioException;
        }
    }


    public static String readFromHdfs(String filePath) throws IOException {
        List<String> lines = new ArrayList<>();
        try {
            Configuration conf = new Configuration();
            String[] filePathSplit = filePath.split("/");
            conf.set("fs.defaultFS", filePathSplit[0] + "//" + filePathSplit[2]);
            FileSystem fs = FileSystem.get(conf);
            if (!fs.exists(new Path((filePath)))) {
                throw new FileNotFoundException("hdfs文件不存在！");
            }
            FSDataInputStream inputStream = fs.open(new Path(filePath));
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bf = new BufferedReader(inputStreamReader);
            String str;
            while ((str = bf.readLine()) != null) {
                lines.add(str);
            }
            inputStream.close();
            inputStreamReader.close();
            return StringUtils.join(lines, "\n");
        }
        catch (IOException ioException) {
            LOG.error("文件读取失败，文件路径为 {}", filePath);
            throw ioException;
        }
    }

}
